﻿using System;
using System.Data;
using System.Collections.Generic;
using Catering_Model;
using Catering_Operation;
namespace Catering_Rules
{
    /// <summary>
    /// Recipes
    /// </summary>
    public partial class T_Recipes
    {
        private readonly D_Recipes dal = new D_Recipes();
        public T_Recipes()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(E_Recipes model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_Recipes model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateCateringName(int ID, string CateringName)
        {
            return dal.UpdateCateringName(ID, CateringName);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateIsBiaoZhun(int ID, int Flag)
        {
            return dal.UpdateIsBiaoZhun(ID, Flag);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_Recipes GetModel(int id)
        {

            return dal.GetModel(id);
        }

        /// <summary>
        /// 获取食谱最大的时间
        /// </summary>
        /// <param name="classid"></param>
        /// <returns></returns>
        public DateTime GetRecipesMaxDateTime(int classid)
        {
            DateTime date = DateTime.Now;
            date = dal.GetRecipesMaxDateTime(classid).AddDays(1);
            switch (date.DayOfWeek.ToString())
            {
                case "Monday": return date;
                case "Tuesday": return date.AddDays(6);
                case "Wednesday": return date.AddDays(5);
                case "Thursday": return date.AddDays(4);
                case "Friday": return date.AddDays(3);
                case "Saturday": return date.AddDays(2);
                default: return date.AddDays(1);
            }
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public E_Recipes GetModelByCache(int id)
        {

            string CacheKey = "RecipesModel-" + id;
            object objModel = Catering_DBOpertion.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Catering_DBOpertion.ConfigHelper.GetConfigInt("ModelCache");
                        Catering_DBOpertion.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (E_Recipes)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 获取数据列表 支持排序查询
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="Order"></param>
        /// <returns></returns>
        public DataSet GetList(string strWhere, string Order)
        {
            return dal.GetList(strWhere, Order);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<E_Recipes> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<E_Recipes> DataTableToList(DataTable dt)
        {
            List<E_Recipes> modelList = new List<E_Recipes>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                E_Recipes model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new E_Recipes();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["CateringName"] != null && dt.Rows[n]["CateringName"].ToString() != "")
                    {
                        model.CateringName = dt.Rows[n]["CateringName"].ToString();
                    }
                    if (dt.Rows[n]["ClassID"] != null && dt.Rows[n]["ClassID"].ToString() != "")
                    {
                        model.ClassID = int.Parse(dt.Rows[n]["ClassID"].ToString());
                    }
                    if (dt.Rows[n]["GroupID"] != null && dt.Rows[n]["GroupID"].ToString() != "")
                    {
                        model.GroupID = int.Parse(dt.Rows[n]["GroupID"].ToString());
                    }
                    if (dt.Rows[n]["PID"] != null && dt.Rows[n]["PID"].ToString() != "")
                    {
                        model.PID = int.Parse(dt.Rows[n]["PID"].ToString());
                    }
                    if (dt.Rows[n]["StateDate"] != null && dt.Rows[n]["StateDate"].ToString() != "")
                    {
                        model.StateDate = DateTime.Parse(dt.Rows[n]["StateDate"].ToString());
                    }
                    if (dt.Rows[n]["StopDate"] != null && dt.Rows[n]["StopDate"].ToString() != "")
                    {
                        model.StopDate = DateTime.Parse(dt.Rows[n]["StopDate"].ToString());
                    }
                    if (dt.Rows[n]["Number"] != null && dt.Rows[n]["Number"].ToString() != "")
                    {
                        model.Number = int.Parse(dt.Rows[n]["Number"].ToString());
                    }
                    if (dt.Rows[n]["Back"] != null && dt.Rows[n]["Back"].ToString() != "")
                    {
                        model.Back = dt.Rows[n]["Back"].ToString();
                    }
                    if (dt.Rows[n]["AreaID"] != null && dt.Rows[n]["AreaID"].ToString() != "")
                    {
                        model.AreaID = int.Parse(dt.Rows[n]["AreaID"].ToString());
                    }
                    if (dt.Rows[n]["ClassIDs"] != null && dt.Rows[n]["ClassIDs"].ToString() != "")
                    {
                        model.ClassIDs = int.Parse(dt.Rows[n]["ClassIDs"].ToString());
                    }
                    if (dt.Rows[n]["temp1"] != null && dt.Rows[n]["temp1"].ToString() != "")
                    {
                        model.temp1 = dt.Rows[n]["temp1"].ToString();
                    }
                    if (dt.Rows[n]["temp2"] != null && dt.Rows[n]["temp2"].ToString() != "")
                    {
                        model.temp2 = dt.Rows[n]["temp2"].ToString();
                    }
                    if (dt.Rows[n]["IsBiaoZhun"] != null && dt.Rows[n]["IsBiaoZhun"].ToString() != "")
                    {
                        model.IsBiaoZhun = Boolean.Parse(dt.Rows[n]["IsBiaoZhun"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            return dal.GetList(PageSize, PageIndex, strWhere);
        }

        /// <summary>
        /// 获取符合要求的食谱
        /// </summary>
        public DataSet GetRecipesByInfo(string ClassID, string starttime, string endtime)
        {
            return dal.GetRecipesByInfo(ClassID, starttime, endtime);
        }

        /// <summary>
        /// 通过条件获取原料总和以及折损率
        /// </summary>
        public DataSet GetRecipesYLCount(string RecipesIDlist, string starttime, string endtime)
        {
            return dal.GetRecipesYLCount(RecipesIDlist, starttime, endtime);
        }

        /// <summary>
        /// 通过条件获取原料总和以及折损率
        /// </summary>
        public DataSet GetRecipesYLCount(string whereClause)
        {
            return dal.GetRecipesYLCount(whereClause);
        }

        /// <summary>
        /// 通过条件获取每天每人消耗量
        /// </summary>
        public DataSet GetRecipesYLday(string RecipesIDlist, string starttime, string endtime)
        {
            return dal.GetRecipesYLday(RecipesIDlist, starttime, endtime);
        }

        /// <summary>
        /// 通过条件获取每天每人消耗量
        /// </summary>
        public DataSet GetRecipesYLday(string whereClause)
        {
            return dal.GetRecipesYLday(whereClause);
        }

        #endregion  Method


        #region 扩展方法
        /// <summary>
        /// 获取非个人食谱集合
        /// </summary>
        /// <param name="IDS"></param>
        /// <param name="ProID"></param>
        /// <returns></returns>
        public DataSet GetNotMyList(string IDS, int EditPersonnelID)
        {
            return dal.GetNotMyList(IDS, EditPersonnelID);
        }
        #endregion
    }
}

